package com.ccb.ic.bbs.controller;

import com.ccb.ic.bbs.exception.CustomizeErrorCode;
import com.ccb.ic.bbs.exception.CustomizeException;
import com.ccb.ic.query.bbs.PaginationDTO;
import com.ccb.ic.query.bbs.ResultDTO;
import com.ccb.ic.bbs.provider.UserProvider;
import com.ccb.ic.bbs.service.NotificationService;
import com.ccb.ic.bbs.service.QuestionService;
import com.ccb.ic.domain.bbs.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ProfileController {


    @Autowired
    private QuestionService questionService;

    @Autowired
    private NotificationService notificationService;

    @Value("${vaptcha.vid}")
    private String vaptcha_vid;

    @Autowired
    UserProvider userProvider;

    @GetMapping("/bbs/user/message")
    public Object messeage(HttpServletRequest request,
                           @RequestParam(name = "page", defaultValue = "1") Integer page,
                           @RequestParam(name = "size", defaultValue = "5") Integer size) {
        User user = userProvider.getUser("token");

        if (user == null) {
            throw new CustomizeException(CustomizeErrorCode.NO_LOGIN);
        }
        PaginationDTO paginationDTO = notificationService.list(user.getId(), page, size);
        Map<String, Object> model = new HashMap<>();
        model.put("section", "message");
        model.put("pagination", paginationDTO);
        // model.addAttribute("sectionName", "最新通知");
        model.put("navtype", "notifynav");

        return ResultDTO.okOf(model);
    }

    @GetMapping("/bbs/user/p/{action}")
    public Object p(HttpServletRequest request,
                    @PathVariable(name = "action") String action,
                    @RequestParam(name = "page", defaultValue = "1") Integer page,
                    @RequestParam(name = "size", defaultValue = "5") Integer size) {
        User user = userProvider.getUser("token");

        if (user == null) {
            throw new CustomizeException(CustomizeErrorCode.NO_LOGIN);
        }
        Map<String, Object> model = new HashMap<>();
        if ("myPosts".equals(action)) {
            model.put("section", "myPosts");
            model.put("sectionName", "我的帖子");
            PaginationDTO pagination = questionService.listByUserId(user.getId(), page, size);
            model.put("pagination", pagination);
            model.put("navtype", "communitynav");
        }
        if ("likes".equals(action)) {
            PaginationDTO paginationDTO = questionService.listByExample(user.getId(), page, size, "likes");
            model.put("section", "likes");
            model.put("pagination", paginationDTO);
            model.put("sectionName", "我的收藏");
            model.put("navtype", "communitynav");
        }

        return ResultDTO.okOf(model);
    }


    @GetMapping("/bbs/profile/{action}")
    public Object profile(HttpServletRequest request,
                          @PathVariable(name = "action") String action,
                          @RequestParam(name = "page", defaultValue = "1") Integer page,
                          @RequestParam(name = "size", defaultValue = "5") Integer size) {
        User user = userProvider.getUser("token");

        if (user == null) {
            throw new CustomizeException(CustomizeErrorCode.NO_LOGIN);
        }

        Map<String, Object> model = new HashMap<>();
        if ("questions".equals(action)) {
            model.put("section", "questions");
            model.put("sectionName", "我的提问");
            PaginationDTO pagination = questionService.listByUserId(user.getId(), page, size);
            model.put("pagination", pagination);
            model.put("navtype", "communitynav");
        }
        if ("notifies".equals(action)) {
            PaginationDTO paginationDTO = notificationService.list(user.getId(), page, size);
            model.put("section", "notifies");
            model.put("pagination", paginationDTO);
            model.put("sectionName", "最新通知");
            model.put("navtype", "notifynav");
        }
        if ("likes".equals(action)) {
            PaginationDTO paginationDTO = questionService.listByExample(user.getId(), page, size, "likes");
            model.put("section", "likes");
            model.put("pagination", paginationDTO);
            model.put("sectionName", "我的收藏");
            model.put("navtype", "communitynav");
        }

        return ResultDTO.okOf(model);
    }


    @GetMapping("/bbs/user/set/{action}")
    public Object getSetPage(HttpServletRequest request,
                             @PathVariable(name = "action") String action) {
        User user = userProvider.getUser("token");
        if (user == null) {
            throw new CustomizeException(CustomizeErrorCode.NO_LOGIN);
        }
        Map<String, Object> model = new HashMap<>();
        if ("info".equals(action) || StringUtils.isBlank(action)) {
            model.put("section", "info");
            model.put("sectionName", "我的资料");
            model.put("sectionInfo", "绑定邮箱账号后，您可以使用邮箱账号登录本站，也可用邮箱账号找回密码\n");
            model.put("navtype", "communitynav");
            return ResultDTO.okOf(model);
        }
        if ("account".equals(action)) {
            model.put("section", "account");
            model.put("sectionName", "绑定/更新邮箱账号");
            model.put("sectionInfo", "绑定邮箱账号后，您可以使用邮箱账号登录本站，也可用邮箱账号找回密码\n");
            model.put("navtype", "communitynav");
            model.put("vaptcha_vid", vaptcha_vid);
            return ResultDTO.okOf(model);
        }
        return ResultDTO.okOf(model);
    }


}
